package com.mall.goods.controller;

import com.mall.commons.utils.CommonResult;
import com.mall.goods.service.PmsProductService;
import com.mall.model.dto.PmsProductDto;
import com.mall.model.vo.CommonPage;
import com.mall.model.vo.PmsProductVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/product")
public class PmsProductController {

    @Autowired
    private PmsProductService pmsProductService;

    /**
     * 分页查询商品信息
     *
     * @param dto
     * @return
     */
    @GetMapping("/list")
    public CommonResult list(PmsProductDto dto) {
        log.info("dto的数据:{}", dto);
        CommonPage commonPage = pmsProductService.list(dto);
        return CommonResult.success(commonPage);
    }

    /**
     * 根据商品名称或货号模糊查询
     *
     * @param keyword
     * @return
     */
    @GetMapping("/simpleList")
    public CommonResult simpleList(@RequestParam("keyword") String keyword) {
        List resultList = pmsProductService.simpleList(keyword);
        return CommonResult.success(resultList);
    }

    /**
     * 批量修改删除状态
     *
     * @return
     */
    @PostMapping("/update/deleteStatus")
    public CommonResult deleteStatus(Integer deleteStatus, @RequestParam List<Long> ids) {
        log.info("deleteStatus：{},ids：{}", deleteStatus, ids.toString());
        pmsProductService.deleteStatus(deleteStatus, ids);
        return CommonResult.success(null);
    }

    /**
     * 批量设为新品
     *
     * @return
     */
    @PostMapping("/update/newStatus")
    public CommonResult newStatus(Integer newStatus, @RequestParam List<Long> ids) {
        log.info("newStatus：{},ids：{}", newStatus, ids.toString());
        pmsProductService.newStatus(newStatus, ids);
        return CommonResult.success(null);
    }

    /**
     * 批量上下架商品
     *
     * @return
     */
    @PostMapping("/update/publishStatus")
    public CommonResult publishStatus(Integer publishStatus, @RequestParam List<Long> ids) {
        log.info("publishStatus：{},ids：{}", publishStatus, ids.toString());
        pmsProductService.publishStatus(publishStatus, ids);
        return CommonResult.success(null);
    }

    /**
     * 批量推荐商品
     *
     * @return
     */
    @PostMapping("/update/recommendStatus")
    public CommonResult recommendStatus(Integer recommendStatus, @RequestParam List<Long> ids) {
        log.info("recommendStatus：{},ids：{}", recommendStatus, ids.toString());
        pmsProductService.recommendStatus(recommendStatus, ids);
        return CommonResult.success(null);
    }


    /**
     * 创建商品
     *
     * @return
     */
    @PostMapping("/create")
    public CommonResult create(@RequestBody PmsProductDto pmsProductDto) {
        log.info("pmsProductDto的内容为:{}", pmsProductDto);
        pmsProductService.create(pmsProductDto);
        return CommonResult.success(1);
    }

    /**
     * 更新商品
     *
     * @return
     */
    @PostMapping("/update/{id}")
    public CommonResult update(@PathVariable("id") Long id, @RequestBody PmsProductDto pmsProductDto) {
        pmsProductService.updateProduct(pmsProductDto);
        return CommonResult.success(1);
    }


    /**
     * 根据商品id获取商品编辑信息
     *
     * @param id
     * @return
     */
    @GetMapping("/updateInfo/{id}")
    public CommonResult updateInfo(@PathVariable("id") Integer id) {
        PmsProductVO pmsProductVO = pmsProductService.updateInfo(id);
        return CommonResult.success(pmsProductVO);
    }
}
